package com.wm.framework.advice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.common.core.domain.AjaxResult;
import com.wm.common.core.domain.entity.SysDept;
import com.wm.common.core.domain.entity.SysDictData;
import com.wm.common.core.page.TableDataInfo;
import com.wm.common.utils.DictUtils;
import com.wm.system.service.ISysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.List;

@ControllerAdvice
@Slf4j
@Order(1)
public class ResponseResultAdvice implements ResponseBodyAdvice<Object> {

    @Value("${wm.multiTenant}")
    private boolean tenant;

    @Autowired
    private ISysDeptService deptService;

    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {

        //多租户列表数据处理
        if (tenant) {
            try {
                if (body instanceof Page || body instanceof TableDataInfo || body instanceof AjaxResult) {

                    JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(body));

                    if (body instanceof AjaxResult) {
                        AjaxResult ajax = (AjaxResult)body;
                        if (!(ajax.get("data") instanceof Page)) {
                            return body;
                        } else {
                            jsonObject = (JSONObject)jsonObject.get("data");
                        }
                    }

                    JSONArray jsonArray = (JSONArray)jsonObject.get(body instanceof TableDataInfo? "rows" : "records");
                    jsonArray.forEach(a -> {
                        JSONObject obj = (JSONObject)a;

                        Object createDept = obj.get("createDept");
                        if (createDept != null) {
                            SysDept company = deptService.getCompanyByDeptId((long) createDept);
                            obj.put("company", company.getDeptName());
                        }
                    });
                    return jsonObject;
                }

            } catch (Exception e) {
                log.error("json转换异常");
            }
        }
        return body;
    }
}
